package com.jintoufs.web.action.baseInfo;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.domain.baseinfo.Customer;
import com.jintoufs.domain.constants.MessageConstants;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.baseinfo.CustomerService;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1 0001.
 */
@Controller
@Scope("prototype")
@RequestMapping("/customer")
public class CustomerAction extends SupportAction {
    @Resource
    private CustomerService customerService;

    @RequestMapping("/list")
    public String list() throws IOException {
        return "baseinfo/customer-list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx,
                            String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Customer> list = customerService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "客户")
    public void oper(HttpServletResponse response, String oper, Customer customer) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        try {
            if (oper.equals("del")) {
                customerService.delete(customer.getId());
            } else if (oper.equals("edit")) {
                customerService.update(customer);
            } else if (oper.equals("add")) {
                customerService.add(customer);
            } else {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage(MessageConstants.OPER_ERROR_MESSAGE);
            }
        } catch (Exception e) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage(MessageConstants.SYSTEM_ERROR_MESSAGE);
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }

    @RequestMapping("input")
    public String input(Integer id, Model model) {
        Customer entity = null;
        // 是否为编辑保存
        Boolean isEdit = false;

        if (id != null && id != 0) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("id", id);
            List<Customer> list = customerService.queryByParams(params);
            if (list != null && list.size() > 0) {
                entity = list.get(0);
            }
            isEdit = true;
        }
        int orgId = 0;
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("entity", entity);

        return "baseinfo/customer-input";
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除客户")
    public void delete(HttpServletResponse response, int[] ids) {
        for (int id : ids) {
            customerService.delete(id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

}
